package vista;

import controlador.ControladorBotones;
import modelo.ErrorSintaxis;
import modelo.Expresion;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;

public class PanelStandard extends JPanel
{

    private Pantalla entrada;
    private ControladorBotones controlador;

    public PanelStandard(Pantalla input)
    {
        this.entrada = input;
        this.controlador = new ControladorBotones(input);
        setBorder(UIUtils.createBorderTitle("Estandar"));
        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        JPanel panelCentral = new JPanel();
        panelCentral.setFocusable(false);
        panelCentral.setLayout(new BoxLayout(panelCentral, BoxLayout.X_AXIS));
        panelCentral.add(createNumberPanel());
        panelCentral.add(createOperatorsPanel());
        add(panelCentral, BorderLayout.CENTER);
    }

    private JPanel createNumberPanel()
    {
        JPanel panelNummeros = new JPanel(new GridLayout(4, 3, 5, 5));
        panelNummeros.setFocusable(false);
        panelNummeros.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        for (int i = 1; i < 10; i++)
        {
            JButton botonNumero = new JButton(String.valueOf(i));
            botonNumero.setFocusable(false);
            botonNumero.addActionListener(controlador);
            panelNummeros.add(botonNumero);
        }

        JButton number = new JButton(String.valueOf(0));
        number.addActionListener(controlador);
        panelNummeros.add(number);

        JButton ans = new JButton("Ans");
        ans.setFocusable(false);
        ans.addActionListener(controlador);
        panelNummeros.add(ans);

        JButton pi = new JButton("π");
        pi.setFocusable(false);
        pi.addActionListener(controlador);
        panelNummeros.add(pi);

        this.entrada.addKeyListener(new KeyAdapter()
        {
            @Override
            public void keyPressed(KeyEvent ke)
            {
                if (ke.getKeyCode() == KeyEvent.VK_ENTER)
                {
                    String expresion = entrada.getText();
                    expresion = expresion.replace("√", "sqrt");
                    expresion = expresion.replace("π", Double.toString(Math.PI));
                    expresion = expresion.replace("sin", "Sin");
                    expresion = expresion.replace("!", "Fact");
                    expresion = expresion.replace("exp", "e");
                    expresion = expresion.replace("Ans", Double.toString(controlador.ultimoValor));

                    try
                    {

                        Expresion manejador = new Expresion(expresion);
                        controlador.ultimoValor = manejador.evaluarExpresion();
                        entrada.setText(Double.toString(controlador.ultimoValor));

                    } catch (ErrorSintaxis ex)
                    {
                        if (ex.getMessage().equalsIgnoreCase("infinity"))
                        {
                            entrada.setText(ex.getMessage());
                        } else
                        {
                            JOptionPane.showMessageDialog(null, ex.getMessage(), "Oops", JOptionPane.INFORMATION_MESSAGE);
                            entrada.setText("");
                        }
                    }
                }
            }
        });

        return panelNummeros;
    }

    private JPanel createOperatorsPanel()
    {

        JPanel operators = new JPanel(new GridLayout(5, 3, 5, 5));
        operators.setFocusable(false);
        operators.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        List<JButton> buttons = new ArrayList<JButton>();
        buttons.add(new JButton("/"));
        buttons.add(new JButton("CE"));
        buttons.add(new JButton("->"));
        buttons.add(new JButton("*"));
        buttons.add(new JButton("("));
        buttons.add(new JButton(")"));
        buttons.add(new JButton("-"));
        buttons.add(new JButton("√x"));
        buttons.add(new JButton("^"));
        buttons.add(new JButton("+"));
        buttons.add(new JButton("."));
        buttons.add(new JButton("="));
        buttons.add(new JButton("<"));
        buttons.add(new JButton(">"));

        for (int i = 0; i < buttons.size(); i++)
        {
            buttons.get(i).addActionListener(controlador);
            buttons.get(i).setFocusable(false);
            operators.add(buttons.get(i));
        }

        return operators;
    }
}